<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>知乎数据采集系统 - 数据可视化</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            height: 100vh;
            overflow-x: hidden;
        }

        .navbar {
            width: 250px;
            height: 100vh;
            background-color: #2c3e50;
            color: white;
            transition: all 0.3s ease;
            position: fixed;
            z-index: 1000;
            overflow-y: auto;
        }

        .navbar.collapsed {
            width: 60px;
        }

        .navbar .logo {
            display: flex;
            align-items: center;
            padding: 20px;
            cursor: pointer;
        }

        .navbar .logo img {
            width: 30px;
            height: 30px;
            margin-right: 10px;
        }

        .navbar .logo span {
            display: inline-block;
            font-size: 18px;
            font-weight: bold;
        }

        .navbar ul {
            list-style-type: none;
            padding: 0;
            margin: 0;
        }

        .navbar li {
            position: relative;
        }

        .navbar li a {
            display: flex;
            align-items: center;
            padding: 15px 20px;
            text-decoration: none;
            color: white;
            transition: background-color 0.3s;
        }

        .navbar li a i {
            margin-right: 10px;
        }

        .navbar li a span {
            display: inline-block;
            font-size: 16px;
        }

        .navbar li a:hover {
            background-color: #34495e;
        }

        .navbar.collapsed li a span {
            display: none;
        }

        .navbar .toggle-btn {
            position: absolute;
            top: 10px;
            right: 10px;
            cursor: pointer;
            color: white;
        }

        .navbar .toggle-btn i {
            font-size: 20px;
        }

        input[type="checkbox"] {
            display: none;
        }

        input[type="checkbox"]:checked+.navbar {
            width: 60px;
        }

        input[type="checkbox"]:checked+.navbar .logo span {
            display: none;
        }

        input[type="checkbox"]:checked+.navbar li a span {
            display: none;
        }

        @media (max-width: 768px) {
            .navbar {
                width: 60px;
            }

            .navbar .logo span,
            .navbar li a span {
                display: none;
            }
        }

        .answer-box {
            margin-left: 265px;
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ddd;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin-top: 10px;
            margin-bottom: 20px;
            width: calc(100% - 275px);
            transition: margin-left 0.3s ease;
        }

        input[type="checkbox"]:checked~.answer-box {
            margin-left: 70px;
            width: calc(100% - 80px);
        }

        .answer-box header {
            text-align: center;
            padding: 20px;
            border-bottom: 1px solid #eaeaea;
            margin-bottom: 20px;
        }

        .answer-box h1 {
            color: #333;
            margin: 0;
            font-size: 2.5em;
        }

        .answer-box table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .answer-box th,
        .answer-box td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #eaeaea;
        }

        .answer-box th {
            background-color: #f0f0f0;
            color: #333;
        }

        .answer-box tr:hover {
            background-color: #f9f9f9;
        }

        .answer-box td.content {
            word-wrap: break-word;
            max-height: 200px;
            overflow-y: auto;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }

        .pagination a {
            padding: 10px 20px;
            margin: 0 5px;
            background-color: #f0f0f0;
            color: #333;
            text-decoration: none;
            border-radius: 4px;
        }

        .pagination a.active {
            background-color: #34495e;
            color: white;
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 1001;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
        }

        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <input type="checkbox" id="checkbox">
    <label for="checkbox" class="toggle-btn">
        <i class="fa fa-bars" aria-hidden="true"></i>
    </label>
    <div class="navbar">
        <div class="logo">
            <img src="./images/1.png" alt="Logo">
            <span>知乎数据采集系统</span>
        </div>
        <ul>
            <li>
                <a href="/User.html">
                    <i class="fa fa-home" aria-hidden="true"></i>
                    <span>提问者个人信息</span>
                </a>
            </li>
            <li>
                <a href="/Question.html">
                    <i class="fa fa-sitemap" aria-hidden="true"></i>
                    <span>问题内容</span>
                </a>
            </li>
            <li>
                <a href="/test.html">
                    <i class="fa fa-user-circle-o" aria-hidden="true"></i>
                    <span>回答内容</span>
                </a>
            <li>
                <a href="/AnalysisChart.html">
                    <i class="fa fa-shopping-bag" aria-hidden="true"></i>
                    <span>点赞分析图</span>
                </a>
            </li>
            <li>
                <a href="/CommentChart.html">
                    <i class="fa fa-th-large" aria-hidden="true"></i>
                    <span>评论分析图</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <i class="fa fa-th-large" aria-hidden="true"></i>
                    <span>内容分析图</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <i class="fa fa-th-large" aria-hidden="true"></i>
                    <span>内容词云图</span>
                </a>
            </li>
        </ul>
    </div>

    <div class="answer-box">
        <header>
            <h1>回答内容</h1>
        </header>
        <table>
            <thead>
                <tr>
                    <th>回答ID</th>
                    <th>作者昵称</th>
                    <th>点赞数</th>
                    <th>评论数</th>
                    <th>发布时间</th>
                    <th>内容</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for answer in answers %}
                <tr>
                    <td>{{ answer.AnswerID }}</td>
                    <td>{{ answer.UserID }}</td>
                    <td>{{ answer.VoteupCount }}</td>
                    <td>{{ answer.CommentCount }}</td>
                    <td>{{ answer.PublicationTime }}</td>
                    <td class="content">{{ answer.Content }}</td>
                    <td>
                        <button class="add-btn" data-id="{{ answer.AnswerID }}">添加</button>
                        <button class="edit-btn" data-id="{{ answer.AnswerID }}">修改</button>
                        <button class="delete-btn" data-id="{{ answer.AnswerID }}">删除</button>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        <div class="pagination">
            {% for page in range(1, total_pages + 1) %}
            <a href="/test.html?page={{ page }}" class="{{ 'active' if page == current_page else '' }}">{{ page }}</a>
            {% endfor %}
        </div>
    </div>

    <!-- 添加回答的模态框 -->
    <div id="add-modal" class="modal">
        <div class="modal-content">
            <span class="close">&times;</span>
            <form id="add-form">
                <label for="add-user-id">作者昵称:</label>
                <input type="text" id="add-user-id" name="add-user-id" required>
                <label for="add-voteup-count">点赞数:</label>
                <input type="number" id="add-voteup-count" name="add-voteup-count" required>
                <label for="add-comment-count">评论数:</label>
                <input type="number" id="add-comment-count" name="add-comment-count" required>
                <label for="add-publication-time">发布时间:</label>
                <input type="datetime-local" id="add-publication-time" name="add-publication-time" required>
                <label for="add-content">内容:</label>
                <textarea id="add-content" name="add-content" required></textarea>
                <button type="submit">添加回答</button>
            </form>
        </div>
    </div>

    <!-- 修改回答的模态框 -->
    <div id="edit-modal" class="modal">
        <div class="modal-content">
            <span class="close">&times;</span>
            <form id="edit-form">
                <input type="hidden" id="edit-answer-id" name="edit-answer-id">
                <label for="edit-answer-id">回答ID:</label>
                <input type="text" id="edit-answer-id-display" name="edit-answer-id-display" readonly>
                <label for="edit-user-id">作者昵称:</label>
                <input type="text" id="edit-user-id" name="edit-user-id" required>
                <label for="edit-voteup-count">点赞数:</label>
                <input type="number" id="edit-voteup-count" name="edit-voteup-count" required>
                <label for="edit-comment-count">评论数:</label>
                <input type="number" id="edit-comment-count" name="edit-comment-count" required>
                <label for="edit-publication-time">发布时间:</label>
                <input type="datetime-local" id="edit-publication-time" name="edit-publication-time" required>
                <label for="edit-content">内容:</label>
                <textarea id="edit-content" name="edit-content" required></textarea>
                <button type="submit">保存修改</button>
            </form>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            // 打开添加模态框
            document.querySelectorAll('.add-btn').forEach(button => {
                button.addEventListener('click', function () {
                    document.getElementById('add-modal').style.display = 'block';
                });
            });

            // 关闭模态框
            document.querySelectorAll('.close').forEach(closeBtn => {
                closeBtn.addEventListener('click', function () {
                    document.getElementById('add-modal').style.display = 'none';
                    document.getElementById('edit-modal').style.display = 'none';
                });
            });

            // 添加回答
            document.getElementById('add-form').addEventListener('submit', function (event) {
                event.preventDefault();
                const userId = document.getElementById('add-user-id').value;
                const voteupCount = document.getElementById('add-voteup-count').value;
                const commentCount = document.getElementById('add-comment-count').value;
                const publicationTime = document.getElementById('add-publication-time').value;
                const content = document.getElementById('add-content').value;

                fetch('/add_answer', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        UserID: userId,
                        VoteupCount: voteupCount,
                        CommentCount: commentCount,
                        PublicationTime: publicationTime,
                        Content: content
                    })
                }).then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            alert('添加成功');
                            location.reload();
                        } else {
                            alert('添加失败: ' + data.message);
                        }
                    });
            });

            // 修改回答
            document.getElementById('edit-form').addEventListener('submit', function (event) {
                event.preventDefault();
                const answerId = document.getElementById('edit-answer-id').value;
                const userId = document.getElementById('edit-user-id').value;
                const voteupCount = document.getElementById('edit-voteup-count').value;
                const commentCount = document.getElementById('edit-comment-count').value;
                const publicationTime = document.getElementById('edit-publication-time').value;
                const content = document.getElementById('edit-content').value;

                fetch('/update_answer', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        AnswerID: answerId,
                        UserID: userId,
                        VoteupCount: voteupCount,
                        CommentCount: commentCount,
                        PublicationTime: publicationTime,
                        Content: content
                    })
                }).then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            alert('修改成功');
                            location.reload();
                        } else {
                            alert('修改失败: ' + data.message);
                        }
                    });
            });

            // 删除回答
            document.querySelectorAll('.delete-btn').forEach(button => {
                button.addEventListener('click', function () {
                    const answerId = this.getAttribute('data-id');
                    if (confirm('确定要删除这条回答吗？')) {
                        fetch('/delete_answer', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify({ AnswerID: answerId })
                        }).then(response => response.json())
                            .then(data => {
                                if (data.success) {
                                    alert('删除成功');
                                    location.reload();
                                } else {
                                    alert('删除失败: ' + data.message);
                                }
                            });
                    }
                });
            });

            // 修改按钮点击事件
            document.querySelectorAll('.edit-btn').forEach(button => {
                button.addEventListener('click', function () {
                    const answerId = this.getAttribute('data-id');
                    const row = this.closest('tr');
                    const userId = row.cells[1].textContent;
                    const voteupCount = row.cells[2].textContent;
                    const commentCount = row.cells[3].textContent;
                    const publicationTime = row.cells[4].textContent;
                    const content = row.cells[5].textContent;

                    document.getElementById('edit-answer-id').value = answerId; // 隐藏字段
                    document.getElementById('edit-answer-id-display').value = answerId; // 显示字段
                    document.getElementById('edit-user-id').value = userId;
                    document.getElementById('edit-voteup-count').value = voteupCount;
                    document.getElementById('edit-comment-count').value = commentCount;
                    document.getElementById('edit-publication-time').value = publicationTime;
                    document.getElementById('edit-content').value = content;

                    document.getElementById('edit-modal').style.display = 'block';
                });
            });
        });
    </script>
</body>


</html>